package com.kun.framework.core.util;

import com.kun.framework.core.enums.ImageKeyEnum;
import javafx.scene.canvas.Canvas;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 内部图片文件工具类
 *
 * @author gzc
 * @since 2024/8/8
 **/
public abstract class InnerImageStore {
    private static final Logger log = LoggerFactory.getLogger(InnerImageStore.class);

    /**
     * 图片文件基础目录
     */
    private static final String DIR = "image/";
    /**
     * 图形对象集合
     */
    private static final Map<ImageKeyEnum, Image> IMAGE_MAP = new HashMap<>(16);

    /**
     * 获取图形对象
     *
     * @param keyEnum 图片主键枚举
     * @return 图形对象
     */
    public static Image getImage(ImageKeyEnum keyEnum) {
        if (keyEnum == null) {
            return null;
        }
        Image image = IMAGE_MAP.get(keyEnum);
        if (image == null) {
            synchronized (InnerImageStore.class) {
                if (image == null) {
                    String path = DIR + keyEnum.getImageFileName();
                    InputStream inputStream = ResourcesLoader.load(path);
                    if (inputStream == null) {
                        return null;
                    }
                    try {
                        image = new Image(inputStream, 0, 0, true, true);
                        IMAGE_MAP.put(keyEnum, image);
                    } finally {
                        try {
                            inputStream.close();
                        } catch (IOException e) {
                            log.error("关闭输入流失败 -> ", e);
                        }
                    }
                }
            }
        }
        return image;
    }

    /**
     * 获取图片视图
     *
     * @param imageKeyEnum 图片主键枚举
     * @return 图片视图
     */
    public static ImageView getImageView(ImageKeyEnum imageKeyEnum) {
        Image image = getImage(imageKeyEnum);
        ImageView imageView = new ImageView(image);
        imageView.setFitHeight(25);
        imageView.setFitWidth(25);
        // 平滑
        imageView.setSmooth(true);
        // 缓存
        imageView.setCache(true);
        // 保持图片横纵比
        imageView.setPreserveRatio(true);
        return imageView;
    }

    /**
     * 获取图标画布对象
     *
     * @param imageKeyEnum 图片主键枚举
     * @return 图标画布对象
     */
    public static Canvas getCanvas(ImageKeyEnum imageKeyEnum) {
        Image image = getImage(imageKeyEnum);
        if (image == null) {
            return null;
        }
        Canvas canvas = new Canvas(image.getWidth() + 2, image.getHeight() + 2);
        PixelWriter pixelWriter = canvas.getGraphicsContext2D().getPixelWriter();
        pixelWriter.setPixels(1, 1, (int) image.getWidth(), (int) image.getHeight(),
                image.getPixelReader(), 0, 0);
        return canvas;
    }
}
